package com.melmar.logcomparator;

import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Iterator;

import org.apache.log4j.FileAppender;
import org.apache.log4j.LogManager;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultCDATA;

public class Log4jComparator {
	
	public Document readXML(String file) {
		SAXReader xmlReader = new SAXReader();
		Document doc = null;
		
		InputStreamEnumerator en = new InputStreamEnumerator(file); 
		InputStream input = new SequenceInputStream(en); 
		
		try {
	    	  doc = xmlReader.read(input);
		} catch (DocumentException e) {
			  return null;
		}
		
		return doc;
	}
	
	public String equalsAssert(Document expected, Document actual) {
		
		Iterator<?> actualElementIt = actual.getRootElement().elements().iterator();
		Element actualElement = null;
		Element expectedElement = null;
		
		for(Object o : expected.getRootElement().elements()){
			
			if(actualElementIt.hasNext()) actualElement = (Element) actualElementIt.next();
			else return "Actual log has no more messages";
			
			expectedElement = (Element) o;
			
			if(!expectedElement.attribute("logger").getValue().equals(
					actualElement.attribute("logger").getValue())) 
				return "Expected " + expectedElement.attribute("logger").getValue()
					   + " is " + actualElement.attribute("logger").getValue();

			
			if(!expectedElement.attribute("level").getValue().equals(
					actualElement.attribute("level").getValue())) 
				return "Expected " + expectedElement.attribute("level").getValue()
						   + " is " + actualElement.attribute("level").getValue();
			
			DefaultCDATA cdataExpected = (DefaultCDATA) expectedElement.element("message").content().get(0);
			DefaultCDATA cdataActual = (DefaultCDATA) actualElement.element("message").content().get(0);
			
			if(!cdataExpected.getText().equals(cdataActual.getText())) 
				return "Expected " + cdataExpected.getText()
						   + " is " + cdataActual.getText();
			
			if(!expectedElement.element("locationInfo").attribute("class").getValue().equals(
					actualElement.element("locationInfo").attribute("class").getValue())) 
				return "Expected " + expectedElement.element("locationInfo").attribute("class").getValue()
						   + " is " + actualElement.element("locationInfo").attribute("class").getValue();
			
			if(!expectedElement.element("locationInfo").attribute("method").getValue().equals(
					actualElement.element("locationInfo").attribute("method").getValue())) 
				return "Expected " + expectedElement.element("locationInfo").attribute("method").getValue()
						   + " is " + actualElement.element("locationInfo").attribute("method").getValue();
			
			if(!expectedElement.element("locationInfo").attribute("file").getValue().equals(
					actualElement.element("locationInfo").attribute("file").getValue()))
				return "Expected " + expectedElement.element("locationInfo").attribute("file").getValue()
						   + " is " + actualElement.element("locationInfo").attribute("file").getValue();
		}
		
		return null;
	}

	public void resetAppender(String string) {
		FileAppender fa = (FileAppender) LogManager.getRootLogger().getAppender("XML");
		fa.activateOptions();
	}




}
